package weekone;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ThreeSum_15 {
    List<List<Integer>> resList = new ArrayList<>();



    public List<List<Integer>> threeSum(int[] nums) {
        int len = nums.length;
        if (len<3)
            return resList;
        Arrays.sort(nums);
        for(int i=0; i<len-2; i++){
            if(nums[i]>0) break;
            twoSum(nums, i+1, -nums[i]);
        }

        return resList;
    }

    private void twoSum(int[] numbers, int start, int target) {

        int i = start, j = numbers.length  - 1;

        while(i<j){
            if(numbers[i]+numbers[j]>target)
                j--;
            else if(numbers[i]+numbers[j]<target)
                i++;
            else {
                Integer[] array = {numbers[start-1], numbers[i], numbers[j]};
                resList.add(Arrays.asList(array));
            }
        }

    }
}
